package me.minecraft.plugin.itemsave.command;

import me.minecraft.plugin.itemsave.manager.ItemManager;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import java.util.*;

/**
 * @author langle__
 * @version 1.0
 */
public class GiveCommand extends SubCommand {

    @Override
    public void onCommand(CommandSender sender, String[] args) {
        if (args.length < 1) {
            sender.sendMessage("[§6ItemSave§f] §c缺少玩家名称");
            return;
        }

        GiveMode mode;
        String playerName = args[0];
        if ("@a".equalsIgnoreCase(playerName)) {
            mode = GiveMode.ALL;
        } else if ("@r".equalsIgnoreCase(playerName)) {
            mode = GiveMode.RANDOM;
        } else if ("@s".equalsIgnoreCase(playerName)) {
            mode = GiveMode.SELF;
        } else {
            mode = GiveMode.TARGET;
        }

        if (args.length < 2) {
            sender.sendMessage("[§6ItemSave§f] §c缺少物品名称");
            return;
        }
        String name = args[1];

        if (!ItemManager.INSTANCE.exists(name)) {
            sender.sendMessage("[§6ItemSave§f] §c物品 " + name + " 不存在");
            return;
        }

        ItemStack item = ItemManager.INSTANCE.getItem(name);
        if (item == null) {
            sender.sendMessage("[§6ItemSave§f] §c物品 " + name + " 不存在");
            return;
        }

        int count = 1;
        if (args.length > 2) {
            try {
                count = Integer.parseInt(args[2]);
            } catch (NumberFormatException e) {
                sender.sendMessage("[§6ItemSave§f] §c无效数量 " + args[2]);
                return;
            }
        }

        item.setAmount(count);

        if (mode == GiveMode.ALL) {
            Collection<? extends Player> players = Bukkit.getOnlinePlayers();
            for (Player player : players) givePlayer(player, item);
            sender.sendMessage("[§6ItemSave§f] 已给予 " + players.size() + " 个玩家 物品 " + name + " 数量 " + count);
        } else {
            Player player;
            if (mode == GiveMode.RANDOM) {
                List<Player> players = new ArrayList<>(Bukkit.getOnlinePlayers());
                if (players.isEmpty()) {
                    sender.sendMessage("[§6ItemSave§f] 未找到玩家");
                    return;
                }
                player = players.get(new Random().nextInt(players.size()));
                if (player == null) {
                    sender.sendMessage("[§6ItemSave§f] 未找到玩家");
                    return;
                }
            } else if (mode == GiveMode.SELF) {
                if (!(sender instanceof Player)) {
                    sender.sendMessage("[§6ItemSave§f] 未找到玩家");
                    return;
                }
                player = (Player) sender;
            } else {
                player = Bukkit.getPlayer(playerName);
                if (player == null) {
                    sender.sendMessage("[§6ItemSave§f] 未找到 " + playerName + " 玩家");
                    return;
                }
            }
            givePlayer(player, item);
            sender.sendMessage("[§6ItemSave§f] 已给予玩家 " + player.getName() + " 物品 " + name + " 数量 " + count);
        }

    }

    private void givePlayer(Player player, ItemStack item) {
        HashMap<Integer, ItemStack> map = player.getInventory().addItem(item);
        if (!map.isEmpty()) {
            World world = player.getWorld();
            Location location = player.getLocation();
            for (ItemStack stack : map.values()) {
                world.dropItem(location, stack);
            }
        }
    }

    @Override
    public List<String> onTabComplete(CommandSender sender, String[] args) {
        if (args.length == 1) {
            List<String> list = new ArrayList<>(List.of("@a", "@r", "@s"));
            Bukkit.getOnlinePlayers().stream().map(Player::getName).forEach(list::add);
            return list;
        } else if (args.length == 2) {
            return ItemManager.INSTANCE.keySet().stream().toList();
        } else if (args.length == 3) {
            return List.of("1", "2", "4", "8", "16", "32", "64");
        }
        return List.of();
    }

    public enum GiveMode {
        ALL,
        RANDOM,
        TARGET,
        SELF
    }

}
